<%
// Creates a list of API Interfaces sorted alphabeticlly

var APIHref = $0 || '/en-US/docs/Web/API';

// Prepares the title of the link. This includes wrapping the appropriate
// portion of the title with <code>. Returns null if the page should not
// be included in the index.

function buildTitle(base) {
    var spaceIndex = base.indexOf(' ');

    // If there are no spaces in the page's title, the entire base
    // string is the title, so we just wrap the whole thing in <code> and
    // return it.

    if ((spaceIndex == -1) && (base != "Reference")) {
        return "<code>" + base + "</code>";
    }

    // If the character after the space is '(', then this is assumed to be an
    // interface with a qualifier after its name (such as "(Firefox OS)").
    // These are allowed to appear in the index, but we end the <code> block
    // before the first space

    if ((spaceIndex < base.length-1) && (base[spaceIndex+1] == '(')) {
        return "<code>" + base.substring(0, spaceIndex) + "</code>" + base.substring(spaceIndex);
    }

    // It's not an interface, so return null.

    return null;
}

function containsTag(tagList, tag) {
    if (!tagList || tagList == undefined) return 0;
    tag = tag.toLowerCase();
    for (var i = 0, len = tagList.length; i < len; i++) {
        if (tagList[i].toLowerCase() == tag) return 1;
    }
    return 0;
}

var pages = await page.subpagesExpand(APIHref); // get subpages, including tags

// The result of `subpagesExpand()` does not guarantee the order to be
// alphabetical. Most of the time it is because of how the OS is iterating over
// files on disk. So sort it first, because the rendering loop (see far below)
// depends on it.
pages.sort((a, b) => a.title.toLowerCase().localeCompare(b.title.toLowerCase()));

var locale = env.locale;
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var html = "";

var numLetters = letters.length;
var numPages = pages.length;
var p = 0;


for (var i = 0; i < numLetters; i++) {
    var curLetter = letters[i];
    var insertedHeading = false; // Haven't done this letter's heading yet

    if (p < numPages) {
        do {
            var tags = pages[p].tags;
            var url = pages[p].url;
            var title = pages[p].title;

            // Build the formatted title string; skip this page if it's not
            // an interface.

            var builtTitle = buildTitle(title);
            if (builtTitle == null) {
                p++;
                continue;
            }

            var badge = "";

            // add badges if needed

            if (containsTag(tags, "Non-standard") || containsTag(tags, "Non standard")) {
                badge = " " + await template("NonStandardBadge", ["1"]);
            }

            if (containsTag(tags, "Obsolete")) {
                badge += " " + await template("ObsoleteBadge", [1]);
            } else if (containsTag(tags, "Deprecated")) {
                badge += " " + await template("DeprecatedBadge", [1]);
            }

            if (containsTag(tags, "Experimental")) {
                badge += " " + await template("ExperimentalBadge", ["1"]);
            }

            // Wrap the badges in another span

            if (badge.length) badge = '<span class="indexListBadges">' + badge + "</span>";

            if (title[0].toUpperCase() == curLetter) {
                if (!containsTag(tags, "junk")) {
                    if (!insertedHeading) {
                        html += "<span>" + curLetter + "</span><ul>";
                        insertedHeading = true;
                    }
                    html += '\n<li><span class="indexListRow"><span class="indexListTerm"><a href="' + url.replace("en-US", locale) + '">' + builtTitle + '</a></span>' + badge + '</span></li>';
                }
                p++;
            } else {
                break;
            }
        } while (p < pages.length);
    }
    html += "\n</ul>\n";
}


%><div class="index">
<%-html%>
</div>
